package com.wanjia.seal.manager.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanjia.seal.manager.annotation.LoginToken;
import com.wanjia.seal.manager.controller.ex.OperatorIdDuplicateException;
import com.wanjia.seal.manager.controller.ex.SealcardNotFoundException;
import com.wanjia.seal.manager.entity.*;
import com.wanjia.seal.manager.mapper.CustomInforMapper;
import com.wanjia.seal.manager.mapper.DatamodilogMapper;
import com.wanjia.seal.manager.mapper.SealMapper;
import com.wanjia.seal.manager.mapper.SysMenuMapper;
import com.wanjia.seal.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/api")
@Api(tags = "印摸表")
@Slf4j
public class SealController extends BaseController{

    @Autowired
    SealMapper sealMapper;
    @Autowired
    DatamodilogMapper datamodilogMapper;
    @Autowired
    CustomInforMapper customInforMapper;

    @ResponseBody
    @LoginToken
    @PostMapping("/seal")
    @ApiOperation(value = "查询印摸", notes = "查询是否成功")
    public JsonResult<Object> getSeal(@RequestBody CustomInfor customInfor, Integer pageNum, Integer pageSize){
        String account = customInfor.getAccount();
        String bank = customInfor.getBank();
        String customName = customInfor.getCustomName();
        log.error("customName:{}",customName);
        List<String> list = Arrays.asList(bank.split(","));
        List<SealVO> seal1 = sealMapper.getSeal(account,customName,list,pageNum,pageSize);
        int size = seal1.size();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("seal",seal1);
        jsonObject.put("total",size);
        return new JsonResult<>(OK,jsonObject);
    }

   /* @ResponseBody
    @LoginToken
    @PostMapping("/seal")
    @ApiOperation(value = "查询印摸", notes = "查询是否成功")
    public JsonResult<Object> getSeal(@RequestBody CustomInfor customInfor, Integer pageNum, Integer pageSize){
        //String account = seal.getAccount();
        //String bank = seal.getBank();
        String account = customInfor.getAccount();
        String bank = customInfor.getBank();
        String customName = customInfor.getCustomName();
        log.error("customName:{}",customName);
        List<String> list = Arrays.asList(bank.split(","));
        if (customName == null){
            List<SealVO> seal1 = sealMapper.getSeal(account,list,pageNum,pageSize);
            int size = seal1.size();
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("seal",seal1);
            jsonObject.put("total",size);
            return new JsonResult<>(OK,jsonObject);
        }else{
            List<SealVO> seal2 = sealMapper.getSeal2(customName, list, pageNum, pageSize);
            int size = seal2.size();
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("seal",seal2);
            jsonObject.put("total",size);
            return new JsonResult<>(OK,jsonObject);
        }
    }
    */
    @ResponseBody
    @LoginToken
    @DeleteMapping("/seal/id")
    @ApiOperation(value = "删除印摸", notes = "删除印摸是否成功")
    public JsonResult deleteSeal(String Account){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Account",Account);
        int rows = sealMapper.delete(wrapper);
        customInforMapper.delete(wrapper);
        if (rows==0 ){
            throw new SealcardNotFoundException("印鉴卡号不存在");
        }
        return new JsonResult<>(OK);
    }

    @ResponseBody
    @PostMapping("/seal/id")
    @LoginToken
    @ApiOperation(value = "新增删除日志", notes = "新增是否成功")
    public JsonResult createSeal(@RequestBody datamodilog datamodilog) {
        String account = datamodilog.getAccount();
        String bank = datamodilog.getBANK();
        String operID = datamodilog.getOperID();
        String modiOper = datamodilog.getModiOper();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Account", account);
        datamodilog aa = new datamodilog();
        aa.setDummy(0);//0为删除成功
        aa.setAccount(account);
        aa.setBANK(bank);
        aa.setOperID(operID);
        aa.setModiOper(modiOper);
        aa.setModitime(new Date());
        aa.setDataUpdateMsg("删除成功");
        datamodilogMapper.insert(aa);
        return new JsonResult(OK);
        }



    @ResponseBody
    @LoginToken
    @PostMapping("/delseal")
    @ApiOperation(value = "查询删除的印摸", notes = "查询是否成功")
    public JsonResult<Object> getSealDelete(@RequestBody datamodilog datamodilog, String startTime, String endTime, Integer pageNum, Integer pageSize){
        String Account = datamodilog.getAccount();
        //Integer dummy = datamodilog.getDummy();
        String modiOper = datamodilog.getModiOper();
        String operID = datamodilog.getOperID();
        String BANK = datamodilog.getBANK();
        List<String> list = Arrays.asList(BANK.split(","));
        if (startTime==null){
            startTime ="1988-01-01";
        }
        if (endTime==null){
            endTime="2099-01-01";
        }
        List<datamodilogVO2> bySeal = datamodilogMapper.findBySeal(Account, modiOper, operID, list, startTime, endTime, pageNum, pageSize);
        int size = bySeal.size();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",size);
        jsonObject.put("datamodilog",bySeal);
        return new JsonResult<>(OK,jsonObject);
    }
}
